<template>
  <div class="login-wrap">
    <div class="ms-title">登 录</div>
    <div class="ms-login">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="0px"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            size="medium"
            suffix-icon="iconfont icon-user"
            placeholder="用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            size="medium"
            suffix-icon="iconfont icon-mima"
            v-model="ruleForm.password"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox :checked="true" label="自动登录" name="type"></el-checkbox>
          <a class="forgetPsd" @click="forgetPsd">没有账号，请注册</a>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data: function() {
    return {
      ruleForm: {
        username: "",
        password: "",
        code: ""
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      // console.log(123)
      this.$axios.post("/admins/login", {
          account: this.ruleForm.username,
          password: this.ruleForm.password
        })
        .then(res => {
          
          if (res.data.length > 0 && res.data[0].userStatus === 1) {
            this.$refs[formName].validate(valid => {
              if (valid) {
                localStorage.setItem("user", JSON.stringify(res.data[0]));
                 const conte = res.data[0].userType
                if(conte===1){
                  this.$router.push("/home");
                }else if(conte===2){
                  this.$router.push("/firstPage");
                }
              }
            });
            // console.log(11,res.data[0].userStatus)
          } else if (res.data.length < 1) {
             this.$message({
               type:"info",
               message:'登录失败'
             })
            return;
          }else{
               this.$message({
               type:"info",
               message:'账号正在申请中，请耐性等候.......'
             })
          }
          // console.log(res);
        });
      // this.$axios.get('/apply/getApply')
      // .then( res => {
      //   console.log(res)
      // } )
    },
    //忘记密码
    forgetPsd() {
      this.$router.push("/forgetPsd");
    }
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #242f42;
  /* background: url('./static/img/bg.jpg') no-repeat; */
}

.ms-title {
  position: absolute;
  /*top: 50%;*/
  width: 100%;
  margin-top: 60px;
  text-align: center;
  font-size: 35px;
  color: #fff;
}

.ms-login {
  position: absolute;
  left: 50%;
  /*top: 50%;*/
  width: 300px;
  height: 260px;
  margin: 130px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  font-size: 14px;
}

.forgetPsd {
  float: right;
  text-decoration: none;
  color: #409eff;
}
.forgetPsd:hover {
  color: #cd4f39;
  cursor: pointer;
}
</style>
